﻿Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.String
Imports IDServer
Imports System.Text
Imports System.Collections
Imports CrystalDecisions.CrystalReports.Engine


Public Class Frm_INTStorageCubage

#Region " 窗體加載"


    Implements IDServer.IDServer.IDServer


    Public ActionHelp As IDServer.IDServer.ActionBtn = IDServer.IDServer.ActionBtn.ActionHelp
    Private MasterDS As DataSet
    Private SelectNode As TreeNode
    Private Img As ImageList
    Public bFind As Boolean
    Private WithEvents MainForm As Frm_MainForm


    Public Property SelectObject() As Frm_MainForm
        Get
            Return MainForm
        End Get
        Set(ByVal value As Frm_MainForm)
            MainForm = value
        End Set
    End Property

    Public Function ActionAdd() As Boolean Implements IDServer.IDServer.IDServer.ActionAdd

    End Function

    Public Function ActionCancel() As Boolean Implements IDServer.IDServer.IDServer.ActionCancel

    End Function

    Public Function ActionDelete() As Boolean Implements IDServer.IDServer.IDServer.ActionDelete

    End Function

    Public Function ActionEdit() As Boolean Implements IDServer.IDServer.IDServer.ActionEdit

    End Function

    Public Function ActionExport() As Boolean Implements IDServer.IDServer.IDServer.ActionExport

    End Function

    Public Function ActionFilter() As Boolean Implements IDServer.IDServer.IDServer.ActionFilter

    End Function

    Public Function ActionGoFirst() As Boolean Implements IDServer.IDServer.IDServer.ActionGoFirst

    End Function

    Public Function ActionGoLast() As Boolean Implements IDServer.IDServer.IDServer.ActionGoLast

    End Function

    Public Function ActionGoNext() As Boolean Implements IDServer.IDServer.IDServer.ActionGoNext

    End Function

    Public Function ActionGoPrevious() As Boolean Implements IDServer.IDServer.IDServer.ActionGoPrevious

    End Function

    Public Function ActionImport() As Boolean Implements IDServer.IDServer.IDServer.ActionImport

    End Function

    Public Function ActionInitialize() As Boolean Implements IDServer.IDServer.IDServer.ActionInitialize

    End Function

    Public Function ActionPrint() As Boolean Implements IDServer.IDServer.IDServer.ActionPrint
        Print()
    End Function

    Public Function ActionSave() As Boolean Implements IDServer.IDServer.IDServer.ActionSave

    End Function

    Public Function ActionSelect() As Boolean Implements IDServer.IDServer.IDServer.ActionSelect

    End Function

#End Region
    Private Sub selectShortName()
        Try
            Dim Dset As DataSet
            Dim sql As String = ""
            sql = "select distinct fShortName from BSC_StoreHouse where fcode in  (select distinct fcode from BSC_Storage )"
            Dset = Exec(sql, E_ExecType.DataSet)
            Me.CBShortName.DataSource = Dset.Tables(0)
            Me.CBShortName.DisplayMember = "fShortName"

        Catch ex As Exception
            MessageBox.Show("Initialize:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK)
        End Try

    End Sub
    Private Sub SetButton(ByVal type As ButtonType, ByVal IsFind As Boolean)
        Try
            Dim URow As DataGridViewRow = Me.DataGridView1.CurrentRow
            Select Case type
                Case ButtonType.Browse
                    With MainForm
                        .ToolStripAdd.Enabled = False
                        .ToolStripEdit.Enabled = False
                        .ToolStripDelete.Enabled = False
                        .ToolStripSave.Enabled = False
                        .ToolStripCancel.Enabled = False
                        .ToolStripFirst.Enabled = False
                        .ToolStripLast.Enabled = False
                        .ToolStripNext.Enabled = False
                        .ToolStripPrivous.Enabled = False
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True
                    End With
            End Select
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub
    Private Sub BindMasterGrid()
        Dim mTable As New DataTable()
        Try
            With mTable
                .Columns.Add("fShortName")
                .Columns.Add("fstorageId")
                .Columns.Add("fVolume")
                .Columns.Add("cubages")
                .Columns.Add("asd")
            End With
            MasterDS = New DataSet
            MasterDS.Tables.Add(mTable)
            Me.DataGridView1.DataSource = MasterDS.Tables(0)

            With Me.DataGridView1
                For i As Integer = 0 To .Columns.Count - 1
                    Dim mKey As String = .Columns(i).Name
                    Dim CaptionStr As String = BindHashTable(1).Item(mKey)
                    .Columns(i).HeaderText = CaptionStr
                Next
                .AllowUserToAddRows = False
                .AllowUserToDeleteRows = False
                .SelectionMode = DataGridViewSelectionMode.FullRowSelect
            End With

        Catch ex As Exception
            MsgBox("BindDetailGrid", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            mTable = Nothing
        End Try



    End Sub
    Private Function BindHashTable(Optional ByVal Index As Integer = 0) As Hashtable
        Dim mHash As New Hashtable
        Try

            With mHash
                .Add("fShortName", "倉庫名稱")
                .Add("fstorageId", "庫位名稱")
                .Add("fVolume", "總體積")
                .Add("cubages", "已用體積")
                .Add("asd", "剩餘體積")
            End With
            Return mHash
        Catch ex As Exception
            Return Nothing
            MsgBox("BindHashTable" & ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            mHash = Nothing

        End Try
    End Function
    Private Sub Frm_INTStorageCubage_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        selectShortName()
        BindMasterGrid()

    End Sub
    Private Sub Print()
        Dim Frm As New Frm_Wating
        Try

            If MasterDS.Tables(0).Rows.Count <= 0 Then
                MsgBox("對不起，沒有數據可列印！", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                Exit Sub
            End If

            If Me.DataGridView1.Rows.Count <= 0 Then
                MsgBox("對不起，沒有數據可列印！", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                Exit Sub
            End If
            Frm.Show()
            Dim Pathstr As String = ""
            Dim ConfigNode() As String = ReadConfigFile.Split(";")
            Dim mServer As String = ""
            Dim mDataBase As String = ""
            Dim mUserID As String = ""
            Dim mPassword As String = ""
            Dim sql As String = ""

            Pathstr = GetReportPath("Store", "Cubage0.rpt")


            For i As Integer = 0 To ConfigNode.Length - 1
                If ConfigNode(i).Split("=")(0).ToLower = "Server".ToLower Then
                    mServer = ConfigNode(i).Split("=")(1)
                End If
                If ConfigNode(i).Split("=")(0).ToLower = "DataBase".ToLower Then
                    mDataBase = ConfigNode(i).Split("=")(1)
                End If
                If ConfigNode(i).Split("=")(0).ToLower = "Uid".ToLower Then
                    mUserID = ConfigNode(i).Split("=")(1)
                End If
                If ConfigNode(i).Split("=")(0).ToLower = "Pwd".ToLower Then
                    mPassword = ConfigNode(i).Split("=")(1)
                End If
            Next

            Dim myReportDoc As New CrystalDecisions.CrystalReports.Engine.ReportDocument
            Dim myParameterValues As New CrystalDecisions.Shared.ParameterValues
            Dim myDiscreteValue As New CrystalDecisions.Shared.ParameterDiscreteValue

            Dim logonInfo As New CrystalDecisions.Shared.TableLogOnInfo
            Dim table As CrystalDecisions.CrystalReports.Engine.Table
            myReportDoc.Load(Pathstr)

            For Each table In myReportDoc.Database.Tables
                logonInfo = table.LogOnInfo
                With logonInfo.ConnectionInfo
                    .ServerName = mServer
                    .DatabaseName = mDataBase
                    .UserID = mUserID
                    .Password = mPassword
                End With
                table.ApplyLogOnInfo(logonInfo)
            Next


            If Me.TabControl1.SelectedIndex = 0 Then
                If MasterDS.Tables(0).Rows.Count > 0 Then
                    myReportDoc.SetDataSource(MasterDS.Tables(0))
                End If
            Else
                myReportDoc.SetDataSource(CType(DataGridView1.DataSource, DataTable))
                myReportDoc.SetParameterValue("fShortName ", PubUserCode)
                myReportDoc.SetParameterValue("fstorageId ", PubUserCode)

            End If

            My.Forms.Frm_Report.CrystalReportViewer1.ReportSource = myReportDoc
            Frm.Dispose()
            Frm_Report.ShowDialog()

        Catch ex As Exception
            MsgBox("對不起，打印出錯！" & ex.Message & ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub
    Private Sub BtnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnFind.Click
        ActionFind()
    End Sub
    Private Sub ActionFind()
        Try
            Dim sql As String = ""
            Dim WhereStr As String = ""

            If Me.CBShortName.Text.Length > 0 Then
                WhereStr += "and fShortName like '%" & Me.CBShortName.Text.Trim & "%'"
            End If
            If Me.TBStorageId.Text.Length > 0 Then
                WhereStr += "and fstorageId like'%" & Me.TBStorageId.Text.Trim & "%'"
            End If



            sql = "select  DISTINCT fShortName,fstorageId, " & vbCrLf
            sql &= "fVolume =  sum(Isnull(fLength,0)*Isnull(fWidth,0)*Isnull(fHeight,0)) , " & vbCrLf
            sql &= "cubages = sum (isnull (d.cubage,0) * isnull (c.Goodsamount,0)), " & vbCrLf
            sql &= " asd = (sum(Isnull(fLength,0)*Isnull(fWidth,0)*Isnull(fHeight,0)) - isnull(sum (isnull (d.cubage,0) * isnull (c.Goodsamount,0)),0) )" & vbCrLf
            sql &= "from BSC_Storage  a  left join  BSC_StoreHouse b on a.fCode = b.fcode  " & vbCrLf
            sql &= " left join V_Storequery c on a.fstorageId = c.storeplace " & vbCrLf
            sql &= " left join bsc_goods d on  c.BarCode = d.Barcode  where 1=1 " & vbCrLf
            sql &= WhereStr
            sql &= "GROUP BY  fShortName, a.fstorageId " & vbCrLf

            MasterDS = ReturnDataset(sql)
            Me.DataGridView1.DataSource = MasterDS.Tables(0)
            Me.SetButton(ButtonType.Browse, True)

        Catch ex As Exception
            MsgBox("BtnFind_Click:" & ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub
    Private Sub DataGridView1_RowPostPaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint
        Try
            Dim DGView As DataGridView = DirectCast(sender, DataGridView)
            Dim SBrush As New SolidBrush(DGView.RowHeadersDefaultCellStyle.ForeColor)
            e.Graphics.DrawString(Convert.ToString(e.RowIndex + 1, System.Globalization.CultureInfo.CurrentCulture), e.InheritedRowStyle.Font, SBrush, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4)
        Catch ex As Exception
            MessageBox.Show("DataGridViewstorage_RowPostPaint:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick

    End Sub
End Class
